﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Database.Classes;

namespace smallDeimos.Game
{
    public partial class HandleAjaxCalls : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (Request.QueryString["type"] != null && Request.QueryString["type"] == "ajax")
                {
                    HandleAjaxCall();
                }
            }
        }

        private void HandleAjaxCall()
        {
            if (Request.QueryString["operation"] != null)
            {
                switch (Request.QueryString["operation"])
                {
                    case "SysCoords":
                        WriteSysCoords();
                        break;
                    case "FlightTime":
                        CalcFlightTime();
                        break;
                    case "build":
                        Build(); break;
                    default: //do nothing
                        break;
                }
            }
            Response.Flush();
            Response.Close();
        }

        private void Build()
        {
            int sId, pId, q;
            if (int.TryParse(Request.QueryString["sId"], out sId) == false)
                return;
            if (int.TryParse(Request.QueryString["pId"], out pId) == false)
                return;
            if (int.TryParse(Request.QueryString["q"], out q) == false)
                return;
            Database.BuldingTypes what;
            switch (Request.QueryString["what"])
            {
                case "Farm":
                    what = Database.BuldingTypes.Farm; break;
                case "Factory":
                    what = Database.BuldingTypes.Factory; break;
                case "Cybernet":
                    what = Database.BuldingTypes.Cybernet; break;
                case "Lab":
                    what = Database.BuldingTypes.Lab; break;
                case "Starbase":
                    what = Database.BuldingTypes.Starbase; break;
                case "CS": what = Database.BuldingTypes.CS; break;
                case "TR": what = Database.BuldingTypes.Transport; break;
                case "DS": what = Database.BuldingTypes.Destroyer; break;
                case "CR": what = Database.BuldingTypes.Cruiser; break;
                case "BS": what = Database.BuldingTypes.Battleship; break;
                default:
                    return;
            }
            switch (Request.QueryString["use"])
            {
                case "pp":
                    StoredProcedures.Build(sId, pId, what, q);
                    break;
                case "su":
                    break;
                default:
                    return;
            }
        }
        private bool SearchForIntInQueryString(string param, out int value)
        {
            if (Request.QueryString[param] != null)
            {
                if (int.TryParse(Request.QueryString[param], out value) == true)
                    return true;

            }
            value = -1;
            return false;
        }
        private void CalcFlightTime()
        {
            int sId1, sId2, p1, p2, energy, speed, x1, x2, y1, y2;
            if (SearchForIntInQueryString("sId1", out sId1) == true)
            {
                List<int> coords = UserDefinedFunctions.SystemCoordinates(sId1);
                x1 = coords[0]; y1 = coords[1];
            }
            else
                return;
            if (SearchForIntInQueryString("sId2", out sId2) == true)
            {
                List<int> coords = UserDefinedFunctions.SystemCoordinates(sId2);
                x2 = coords[0]; y2 = coords[1];
            }
            else
                return;
            if (SearchForIntInQueryString("p1", out p1) == false)
                return;
            if (SearchForIntInQueryString("p2", out p2) == false)
                return;
            if (SearchForIntInQueryString("energy", out energy) == false)
                return;
            if (SearchForIntInQueryString("speed", out speed) == false)
                return;
            double tt;
            if (UserDefinedFunctions.PlanetOwner(sId1, p1) == UserDefinedFunctions.PlanetOwner(sId2, p2))
                tt = Equations.TravelTime(x1, x2, y1, y2, p1, p2, 1, energy, speed);
            else
                tt = Equations.TravelTime(x1, x2, y1, y2, p1, p2, 0, energy, speed);
            int h = (int)tt;
            int m = (int)(tt - h * 1.0);
            int s = (int)(tt - h * 1.0 - m * 1.0);

            Response.Write(h.ToString() + "h " + m.ToString() + "m " + s.ToString() + "s");
        }

        private void WriteSysCoords()
        {
            int sId;
            if (SearchForIntInQueryString("sId", out sId) == true)
            {
                List<int> coords = UserDefinedFunctions.SystemCoordinates(sId);
                Response.Write(coords[0].ToString() + "," + coords[1].ToString());
            }
        }
    }
}